﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using GTServices.Entities;
using GTServices.Model;
using GTServices.WPF.Client.ViewModels.Entities;
using GTServices.WPF.Client.Extensions;
using GTServices.Model.Extensions;
using GTServices.Model.Models;
using GTServices.WPF.Client.Data;

namespace GTServices.WPF.Client.ViewModels
{
    public class CreateBugViewModel : Screen
    {

        private bool _includeAllCategories;
        public bool IncludeAllCategories
        {
            get { return _includeAllCategories; }
            set
            {
                _includeAllCategories = value;
                NotifyOfPropertyChange(() => IncludeAllCategories);
                NotifyOfPropertyChange(() => Categories); 
            }
        }

        public List<Category> Categories
        {
            get
            {
                if (Bug.MatchInfos != null)
                {
                    IList<Category> categories = GTApplication.Instance.DataModel.Categories.ToList();

                    if (categories != null && categories.Count() > 0)
                    {
                        SelectedCategory = categories.FirstOrDefault();
                        return categories.ToList(); 
                    }
                }
                return null;
            }
        }

        public Category SelectedCategory
        {
            get { return Bug.Category; }
            set
            {
                Bug.Category = value;
                Template = value.Template;
                NotifyOfPropertyChange(() => SelectedCategory);
                NotifyOfPropertyChange(() => Template);
                NotifyOfPropertyChange(() => TemplateText); 
            }
        }

        public Template Template
        {
            get { return Bug.Template; }
            set
            {
                Bug.Template = value;
                NotifyOfPropertyChange(() => Template);
                NotifyOfPropertyChange(() => TemplateText); 
            }
        }

        public String TemplateText
        {
            get
            {
                if (Bug.Category != null && Bug.Template != null)
                {
                    try
                    {
                        Bug.Text = TemplateTransformationModel.Transform(Bug);
                    }
                    catch (Exception e)
                    {
                        Bug.Text = e.Message;
                    }
                }
                return Bug.Text;
            }
            set
            {
                if (Bug != null)
                {
                    Bug.Text = value;
                    NotifyOfPropertyChange(() => TemplateText);
                }
            }
        }

        public Bug _bug;
        public Bug Bug
        {
            get { return _bug; }
            set
            {
                _bug = value;
                NotifyOfPropertyChange(() => Bug);
                NotifyOfPropertyChange(() => TemplateText); 
            }
        }
        private  TemplateTransformationModel TemplateTransformationModel { get; set; } 

        public CreateBugViewModel(){
           TemplateTransformationModel  = new TemplateTransformationModel();
        }

        public void SelectTemplate()
        {
            //Get all the patterns not associated with another issue. 
            var templateViewModels = ModelViewModels.Instance.TemplateViewModels.ToList();
            //Going to need to add a way to filter. 
            ListPickerViewModel lpvm = new ListPickerViewModel(templateViewModels as System.Collections.IList, "Template.Name", "Template.Name" ,System.Windows.Controls.SelectionMode.Single);

            if (GTWindowManager.Instance.ShowDialog(lpvm, 300, 300) == true && lpvm.SelectedItems != null && lpvm.SelectedItems.Count > 0)
            {
                Template = (lpvm.SelectedItem as TemplateViewModel).Template;
                NotifyOfPropertyChange(() => Template); 
            }
        }

        public void SelectCategoryTemplate()
        {
            if (SelectedCategory == null)
                return; 

            //Get all the patterns not associated with another issue. 
            var templateViewModels = ModelViewModels.Instance.TemplateViewModels.ToList();
            //Going to need to add a way to filter. 
            ListPickerViewModel lpvm = new ListPickerViewModel(templateViewModels as System.Collections.IList, "Template.Name", "Template.Name", System.Windows.Controls.SelectionMode.Single);

            if (GTWindowManager.Instance.ShowDialog(lpvm, 300, 300) == true && lpvm.SelectedItems != null && lpvm.SelectedItems.Count > 0)
            {
                SelectedCategory.Template = (lpvm.SelectedItem as TemplateViewModel).Template;
                NotifyOfPropertyChange(() => SelectedCategory);
                NotifyOfPropertyChange(() => SelectedCategory.Template);
            }
        }

        public void SelectCategoryClassification()
        {
            if (SelectedCategory == null)
                return; 

            //Get all the patterns not associated with another issue. 
            var classificationViewModels = ModelViewModels.Instance.ClassficationViewModels.ToList();
            //Going to need to add a way to filter. 
            ListPickerViewModel lpvm = new ListPickerViewModel(classificationViewModels as System.Collections.IList, "Classification.Name", "Classification.Name", System.Windows.Controls.SelectionMode.Single);

            if (GTWindowManager.Instance.ShowDialog(lpvm, 300, 300) == true && lpvm.SelectedItems != null && lpvm.SelectedItems.Count > 0)
            {
                SelectedCategory.Classification = (lpvm.SelectedItem as ClassificationViewModel).Classification;
                NotifyOfPropertyChange(() => SelectedCategory);
                NotifyOfPropertyChange(() => SelectedCategory.Classification);
            }
        }



        public void Ok()
        {
            TryClose(true); 
        }

        public void Cancel()
        {
            TryClose(false);
        }
    }
}
